package com.shiro.base.service.impl;

import com.shiro.base.dao.BaseDao;
import com.shiro.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * User: dengpeng
 * Date: 2018/7/6
 * Time: 14:43
 * Description: No Description
 */
@SuppressWarnings({ "unchecked", "SpringJavaAutowiredFieldsWarningInspection" })
@Service
public abstract class BaseServiceImpl<Dao extends BaseDao> implements BaseService<Dao> {

    @Autowired
    protected Dao dao;

    /**
     * 根据id查询
     */
    @Override
    public <T, ID> T findById(ID id) {

        Optional<T> optional = dao.findById(id);
        return optional.orElse(null);
    }

    /**
     * 保存单个实体
     */
    @Override
    public <T> void save(T t) {

        dao.save(t);
    }

    /**
     * 批量添加
     */
    @Override
    public <T> void save(List<T> list) {

        dao.saveAll(list);
    }

    /**
     * 根据id删除
     */
    @Override
    public <ID> void delete(ID id) {

        dao.delete(id);
    }
}
